<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class AdministerController extends CommonController{
	private $adminModel;
	private $userroleModel;
	public function __init(){
		parent::__init();
		$this->adminModel = new \Common\Model\Admin;
		$this->userroleModel = new \Common\Model\Userrole;
	}
	public function index(){
		$adminData = $this->adminModel->leftJoin('user_role','aid','=','user_id')->leftJoin('role','role_id','=','id')->where("aid != {$_SESSION['aid']} AND aid != 1")->get();
		View::with('adminData',$adminData);
		View::make();
	}
//	添加管理员
	public function add(){
		if(IS_POST){
			if(!$this->adminModel->store()) View::error($this->adminModel->getError());
			View::success('添加成功！',U('index'));
		}
		View::make();
	}
//	更变权限
	public function edit(){
		$aid = Q('get.aid',0,'intval');
		if(IS_POST){
			$user_id = Q('post.user_id',0,'intval');
			$role_id = Q('post.role_id',0,'intval');
			$data = array(
				'role_id'	=>	$role_id,
				'user_id'	=>	$user_id,
			);
			
			if($this->userroleModel->where("user_id = {$user_id}")->find()){
				$this->userroleModel->where("user_id = {$user_id}")->save($data);
			}else{
				$this->userroleModel->add($data);
			}
			View::success('更改成功',U('index'));
		}
		$adminData = $this->adminModel->leftJoin('user_role','aid','=','user_id')->leftJoin('role','role_id','=','id')->where("aid = $aid")->find();
		$role = Db::table('role')->field('id,name')->get();
		View::with('role',$role);
		View::with('adminData',$adminData);
		View::make();
	}
}
